<!--
 * @author  dumeng
 调查问卷-设置多选题
-->
<template>
  <div class="question-duoxuan">
    <el-form
      ref="duoxuanform"
      :model="baseform"
      :rules="baserules"
      class="dataForm nomt"
      label-position="right"
      label-width="140px"
    >
      <el-row>
        <el-col :span="24">
          <el-form-item label="题干" prop="stem">
            <el-input
              v-model.trim="baseform.stem"
              :rows="2"
              type="textarea"
              placeholder="题干这里填写题目描述"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item
            v-for="(answer, index) in baseform.opts"
            :key="index"
            :label="`选项${index+1}`"
            class="options"
            required
          >
            <el-input v-model.trim="baseform.opts[index]" placeholder="请输入选项内容" @input="changeText($event,index)" />
            <span class="remove-option" @click.prevent="removeOption(index)">
              <i class="el-icon-delete" />
            </span>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-button icon="el-icon-circle-plus" class="add-optiopn" @click="addOption">添加备选答案</el-button>
        </el-col>
        <el-col :span="24">
          <el-form-item label="允许填写其他答案" prop="isOther">
            <el-radio-group v-model="baseform.isOther">
              <el-radio label="1">是</el-radio>
              <el-radio label="0">否</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="是否必答" prop="isMust">
            <el-radio-group v-model="baseform.isMust">
              <el-radio label="1">是</el-radio>
              <el-radio label="0">否</el-radio>
            </el-radio-group>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="最少选择项数">
            <el-col :span="8">
              <el-input-number v-model.trim="limitRange.minOption" style="width: 100%;" placeholder="请输入最少选择项数" controls-position="right" :min="1" :max="baseform.opts.length" />
            </el-col>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="最多选择项数">
            <el-col :span="8">
              <el-input-number v-model.trim="limitRange.maxOption" style="width: 100%;" placeholder="请输入最多选择项数" :disabled="limitRange.optionUpper" controls-position="right" :min="1" :max="baseform.opts.length" />
            </el-col>
            <el-col :span="3" style="text-align: right;">
              <el-checkbox v-model="limitRange.optionUpper" label="无限制">选项总数</el-checkbox>
            </el-col>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="排序" prop="sort">
            <el-input v-model.trim="baseform.sort" placeholder="请输入排序号" />
          </el-form-item>
        </el-col>

      </el-row>
    </el-form>
  </div>
</template>

<script>
import { number } from '@/utils/validate'

export default {
  name: 'Duoxuan',
  props: {
    type: {
      type: String,
      default: '20'
    },
    getData: {
      type: Object,
      default: () => {
      }
    }
  },
  data() {
    return {
      baseform: {
        stem: '',
        opts: ['', '', '', ''],
        isOther: '1',
        isMust: '1',
        sort: ''
      },
      limitRange: {
        optionFloor: false,
        optionUpper: true,
        minOption: '1',
        maxOption: ''
      },
      baserules: {
        stem: [
          { required: true, message: '请输入题干', trigger: 'blur' }
        ],
        opts: [
          { required: true, message: '请输入备选答案', trigger: 'blur' }
        ],
        isOther: [
          { required: true, message: '请选择是否允许填写其他答案', trigger: 'blur' }
        ],
        isMust: [
          { required: true, message: '请选择是否必答', trigger: 'blur' }
        ],
        sort: [
          { required: true, message: '请输入排序号', trigger: 'blur' },
          { validator: number, trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    if (this.getData && this.getData.id) {
      try {
        const answer = JSON.parse(this.getData.answer)
        this.$set(this.baseform, 'stem', this.getData.stem)
        this.$set(this.baseform, 'isMust', this.getData.isMust)
        this.$set(this.baseform, 'isOther', this.getData.isOther)
        this.$set(this.baseform, 'sort', this.getData.sort)
        this.$set(this.baseform, 'opts', answer.opts)
        if (this.getData.questionJson) {
          this.$set(this, 'limitRange', JSON.parse(this.getData.questionJson))
        }
      } catch (error) {
        console.log(error)
        this.$message({
          type: 'error',
          message: '参数异常，请重试!',
          onClose: () => {
            this.$router.back()
          }
        })
      }
    }
  },
  methods: {
    changeText(e, index) {
      if (e === '其他') {
        this.baseform.opts[index] = '其 他'
      }
    },
    /**
			 * 删除指定选项
			 * @param i
			 */
    removeOption(i) {
      // if (this.rightAnswer.indexOf(this.baseform.opts[i]) > -1) {
      //   this.rightAnswer.splice(this.rightAnswer.indexOf(this.baseform.opts[i]), 1)
      // }
      this.baseform.opts.splice(i, 1)
    },
    /**
			 * 添加选项
			 */
    addOption() {
      this.baseform.opts.push('')
    },
    /**
			 * 判断数据中是否含有相同项
			 * @param array
			 * @returns {number}
			 * @constructor
			 */
    checkArrayElement(arr) {
      const array = [...[], ...arr]
      array.sort() // 数组排序
      var reNum = 0 // 返回结果
      // 遍历整个数组对象
      for (var i = 0; i < array.length; i++) {
        // 跳过最后一个元素的比较
        if (i + 1 === array.length) {
          continue
        }
        // 判断相邻的元素是否相同
        if (array[i] === array[i + 1]) {
          reNum += 1
        }
      }
      return reNum
    },
    getPostData() {
      let params = ''
      if (this.checkArrayElement(this.baseform.opts) > 0) {
        this.$message({
          message: '选项内容不能相同！',
          type: 'warning'
        })
        return false
      }
      if (this.baseform.opts.some(item => item === '')) {
        this.$message({
          message: '请填写选项内容！',
          type: 'warning'
        })
        return false
      }
      if (this.baseform.opts.length < 2) {
        this.$message({
          message: '请至少增加两个选项！',
          type: 'warning'
        })
        return false
      }
      this.$refs.duoxuanform.validate(valid => {
        if (valid) {
          const answer = {
            type: this.type,
            opts: this.baseform.opts
          }
          params = {
            answer: JSON.stringify(answer),
            questionJson: JSON.stringify(this.limitRange),
            isMust: this.baseform.isMust,
            isOther: this.baseform.isOther,
            stem: this.baseform.stem,
            sort: this.baseform.sort
          }
        }
      })
      return params
    }
  }
}
</script>

<style lang="scss">
  .question-duoxuan {
    .el-radio-group {
      width: 100%;
    }
    .options .el-form-item__content {
      display: flex;
      align-items: center;
      .el-input,
      .el-checkbox {
        margin-right: 10px;
      }
    }
    .option-rightasnwer {
      display: inline-block;
      flex-shrink: 0;
      font-size: 14px;
      color: green;
      width: 90px;
      margin-right: 20px;
    }
    .order-fixed {
      margin: 10px 0;
      padding-left: 100px;
    }
    .remove-option {
      cursor: pointer;
    }
    .add-optiopn {
      margin-bottom: 10px;
      margin-left: 100px;
    }
    .tip {
      padding-left: 100px;
      padding-bottom: 10px;
      color: red;
    }
  }
</style>
